package com.hushan.standby;

import com.hushan.standby.compoment.LeaderElection;
import org.noear.solon.Solon;
import org.noear.solon.annotation.SolonMain;

import java.io.IOException;

@SolonMain
public class App {
    public static String SOURCE_HOST = null;
    public static String LOCAL_HOST = "127.0.0.1";
    public static String VIRTUAL_IP = "192.168.30.36";
    public static String VIRTUAL_GATEWAY = "192.168.30.1";
    public static String VIRTUAL_MASK = "255.255.255.0";
    public static String VIRTUAL_INTERFACE_NAME = "以太网";

    public static void main(String[] args) throws IOException {
        System.setProperty("user.timezone", "Asia/Shanghai");
        // 从启动参数中获取 SOURCE_HOST
        for (int i = 0; i < args.length; i++) {
            if (args[i].equalsIgnoreCase("--source-host") && i + 1 < args.length) {
                SOURCE_HOST = args[i + 1];
                continue;
            }
            if (args[i].equalsIgnoreCase("--local-host") && i + 1 < args.length) {
                LOCAL_HOST = args[i + 1];
            }
            if (args[i].equalsIgnoreCase("--virtual_ip") && i + 1 < args.length) {
                VIRTUAL_IP = args[i + 1];
            }
            if (args[i].equalsIgnoreCase("--virtual_gateway") && i + 1 < args.length) {
                VIRTUAL_GATEWAY = args[i + 1];
            }
            if (args[i].equalsIgnoreCase("--virtual_mask") && i + 1 < args.length) {
                VIRTUAL_MASK = args[i + 1];
            }
            if (args[i].equalsIgnoreCase("--virtual_interface_name") && i + 1 < args.length) {
                VIRTUAL_INTERFACE_NAME = args[i + 1];
            }
        }
        if (SOURCE_HOST == null) {
            System.err.println("Error: --source-host is not specified.");
            return;
        }

        System.out.println("Using SOURCE_HOST: " + SOURCE_HOST);
        System.out.println("Using LOCAL_HOST: " + LOCAL_HOST);
        // 启动Solon框架，并传入App类、args参数，同时设置app.enableSocketD(true)
        Solon.start(App.class, args, app -> app.enableSocketD(true));
        // 启动主从确认消息
        LeaderElection.startMonitor();
    }
}